package org.liuqm.filter;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.io.Resources;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

public class RequestMappingUtils {




    public static Map<String, String> getUrlMapping() throws IOException {

        Map<String, String> result = new HashMap<String, String>();
        //通过获取当前文件夹下的所有java类包含@RequestMapping注解
        Properties properties = new Properties();
        properties.load(new FileInputStream(Resources.getResourceAsFile("requestMapping.properties")));
        String packName = (String) properties.get("packagename");
        String packStr = RequestMappingUtils.class.getPackage().getName().replace(".", "/");

        //获取需要扫描的文件夹路径
        String dicpath = RequestMappingUtils.class.getResource("").getPath().replace(packStr, packName.replace(".", File.separator));

        File dir = new File(dicpath);

        if (dir.isDirectory()) {
            //获取当前文件夹下所有文件
            File[] files = dir.listFiles();
            List<Class> classList=Arrays.stream(files).filter(file -> {
                String className = file.getName().replace(".class", "");
                try {
                    Class clazz = Class.forName(packName+"." + className);

                    Annotation annotation = clazz.getAnnotation(RequestMapping.class);
                    //没有取到我们自定义的注解
                    if (annotation != null) {
                        return true;
                    }
                    return false;
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }

            }).map(file -> {
                String className = file.getName().replace(".class", "");
                try {
                    Class clazz = Class.forName(packName+"." + className);
                    return clazz;
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }).toList();


            classList.forEach(classInfo -> {

                RequestMapping annotation = (RequestMapping) classInfo.getAnnotation(RequestMapping.class);
                String baseUrl=annotation.value();
                String className = classInfo.getName();

                Method[] methods=classInfo.getMethods();
                for (int i = 0; i <methods.length ; i++) {
                    String methodName = methods[i].getName();
                    RequestMapping annotation2 =methods[i].getAnnotation(RequestMapping.class);
                    if(annotation2!=null){
                        String methodUrl=annotation2.value();
                        result.put(baseUrl+methodUrl,className+"."+methodName);
                    }

                }
            });


        }


        return result;
    }

    /**
     * 通过扫描包路径下的所有文件获得java类
     *
     * @param packName
     * @return
     */
    private List<String> getClassList(String packName) {

        List<String> result = new ArrayList<String>();
        //获取项目的绝对路径
        String rootPath = this.getClass().getResource("org/liuqm/servlet").getPath();


        return result;


    }

    public static void main(String[] args) throws IOException {
        Map result=getUrlMapping();
        System.out.println(JSON.toJSONString(result));
    }
}
